#include <scheduler/scheduler.h>
#include <config/scheduler_conf.h>
#include <common/global_data.h>

#include <scheduler/processor_context.h>
#include <scheduler/scheduler_factory.h>
#include <gtest/gtest.h>
#include <vector>

#include <base/macros.h>
#include <scheduler/processor.h>
#include <scheduler/policy/classic_context.h>

void scheduler_testfunc1() {
    std::cout << "test cheduler func1 --------------------------------" <<std::endl;
}

void scheduler_testfunc2() {
    std::cout << "test cheduler func2 --------------------------------" <<std::endl;
}
void proc() {}

void test_sched_classic(){
    GlobalData::Instance()->SetProcessGroup("example_sched_classic");
    auto sched1 = dynamic_cast<SchedulerClassic*>(Scheduler_Instance());
    std::shared_ptr<CRoutine> cr = std::make_shared<CRoutine>(scheduler_testfunc1);
    auto task_id = GlobalData::RegisterTaskName("ABC");
    cr->set_id(task_id);
    cr->set_name("ABC");

    EXPECT_TRUE(sched1->DispatchTask(cr));
    // dispatch the same task
    EXPECT_FALSE(sched1->DispatchTask(cr));
    //EXPECT_TRUE(sched1->RemoveTask("ABC"));

    std::cout << "finish test cr" << std::endl;

    std::shared_ptr<CRoutine> cr1 = std::make_shared<CRoutine>(scheduler_testfunc2);

    cr1->set_id(GlobalData::RegisterTaskName("xxxxxx"));
    cr1->set_name("xxxxxx");
    EXPECT_TRUE(sched1->DispatchTask(cr1));
    std::cout << "finish test cr1" << std::endl;
    auto t = std::thread(proc);
    sched1->SetInnerThreadAttr("shm", &t);
    if (t.joinable()) {
        t.join();
    }
    std::cout << "-----------Finished Test_scheduler---------" << std::endl;
}

void test_create_task(){
    GlobalData::Instance()->SetProcessGroup("example_sched_classic");
    auto sched = Scheduler_Instance();
    std::cout << "debug" << std::endl;
    // read example_sched_classic.conf task 'ABC' prio
    std::string croutine_name = "A";
  
    EXPECT_TRUE(sched->CreateTask(&proc, croutine_name));
    //create a croutine with the same name
    EXPECT_FALSE(sched->CreateTask(&proc, croutine_name));
  
    auto task_id = GlobalData::RegisterTaskName(croutine_name);
    EXPECT_TRUE(sched->NotifyTask(task_id));
  
    EXPECT_TRUE(sched->RemoveTask(croutine_name));
    // remove the same task twice
    EXPECT_FALSE(sched->RemoveTask(croutine_name));
    // remove a not exist task
    EXPECT_FALSE(sched->RemoveTask("driver"));
}

int main()
{   

    //初始化 global_data
    auto global_data = GlobalData::Instance();
    //test_create_task();
    test_sched_classic();
    // test_create_task();
}
